import router from './router'
import store from './store'
import getPageTitle from './utils/get-page-title'
import { asyncRoutes } from './router/index'
router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  const token = store.state.user.token
  console.log(token)
  if (token) {
    if (to.path === '/login') {
      console.log(1)
      next('/')
    } else {
      const UserInfo = store.state.user.userfrom
      if (!UserInfo.userId) {
        await store.dispatch('user/getuserinfo')
        // console.log(asyncRoutes)
        const list = store.state.user.userfrom.roles.menus
        const newarr = asyncRoutes.filter(item => list.includes(item.children[0].name))
        // console.log(newarr)
        router.addRoutes([...newarr, { path: '*', redirect: '/404', hidden: true }])
        store.commit('menus/setList', [...newarr, { path: '*', redirect: '/404', hidden: true }])
        next({ ...to, replace: true })
        // next()
      } else {
        next()
      }
    }
  } else {
    if (to.path === '/login') {
      console.log('2')
      next()
    } else {
      next('/login')
    }
  }
})
// import router from './router'
// import store from './store'
// // 白名单中的页面
// const whiteList = ['/login','404']
// router.beforeEach((to,from,next)=>{
//   const token=  store.state.user.token
//   console.log(token);
//   if(token){
//     // 是否有token
//     if(to.path === '/login'){
//       next('/') //到主页
//       console.log('你已经登陆 不能访问登陆页')
//     }else{
//       next()
//     }
//   }else{
//     // 是否有白名单
//     if(whiteList.includes(to.path)){
//       next()
//     }else{
//       next('/login')
//     }
//   }

// })
